<template>
	<view class="content">
		<view class="img-box">
			<u--input v-model="title" class="file-tip" placeholder="请输入标题"></u--input>
		</view>
		<view class="">
			<u--textarea v-model="content" placeholder="请输入议题内容" height="150" confirmType="done"></u--textarea>
		</view>
		<view class="img-box">
			<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="8"
				width="105" height="105" uploadText="添加图片" uploadIcon="plus" :previewFullImage="true">
			</u-upload>
		</view>
		<view class="file-box" style="margin-top: 10px;">
			<u-checkbox-group v-model="isSync" placement="column">
				<u-checkbox class="file-tip" :customStyle="{marginBottom: '8px'}" labelColor="#999999" label="是否同步到清外足迹"
					name="1">
				</u-checkbox>
			</u-checkbox-group>
		</view>
		<!-- <view class="file-box content-box ">
			<u-upload :fileList="fileList1" :previewImage="false" @afterRead="afterRead1" @delete="deletePic1"
				accept="file" :maxCount="1" name="1">
				<view class="content-box">
					<view class="file-tip" v-if="!show_files">
						上传附件
					</view>
          <div style="position: relative" v-if="show_files">
            <div class="file-name "   @click.stop="handleDownload(form.files)">
              <u-icon name="attach" color="#4871C0" size="24"></u-icon>
              <label class="file-txt">{{form.files?form.files.filename:""}}</label>
            </div>
          </div>
          <view class="new-tip"  @click="show_files  = !show_files;fileList1=[]">
            更新
          </view>
				</view>
			</u-upload>
		</view> -->
    <view class="tishititle"></view>
		<view class="button" @click="save()">
			提交
		</view>
	</view>
</template>

<script>
	import {
		HTTP_REQUEST_URL,
	} from '@/config/app';
  import {RecordDetail, uploadRecord} from "../../../api/meetApi";
	export default {
		data() {
			return {
				id: '',
				content: '',
				fileList: [],
				fileList1: [],
				images: [],
				files: null,
				fileName: null,
				show_files: false,
				form: {},
        title:'',
        isSync:[]
			}
		},
		onLoad(options) {
			this.id = options.id;
			this.getdetail()
		},
		methods: {
      getdetail() {
        let params = {
          token: uni.getStorageSync('token'),
          meet_id: this.id
        }
        RecordDetail(params).then(res => {
          this.isLoading = false;
          console.log(res)
          if (res.code == 1) {
            this.meetdeal = res.data
           this.content = res.data.content;
             this.title = res.data.title;
             String(res.data.is_sys) == '1'&&String(res.data.is_sys)?this.isSync=['1']:this.isSync=[];
             if(res.data.images){
               this.images = JSON.parse(JSON.stringify(res.data.images));
               this.fileList = JSON.parse(JSON.stringify(res.data.images));
             }
            console.log("===is_sys",this.isSync)

          }
        })
      },
      // 点击附件预览
      handleDownload(item) {
        console.log("=====item",item)
        let params={
          url:item.fullurl, // 需要预览文件的地址(必填，可以使用相对路径)
          name:item.filename, // 需要预览文件的文件名(不填的话取url的最后部分)
          size:item.filesize,// 需要预览文件的字节大小(必填)
          type:item.mimetype
        }
        this.$store.dispatch('yulan',params)
      },
			// 保存
			save() {
				if (!this.content) {
					uni.showToast({
						title: '请填写议题内容',
						icon: 'none',
						duration: 1000
					});
					return;
				}
				if (!this.images.length) {
					uni.showToast({
						title: '请添加图片',
						icon: 'none',
						duration: 1000
					});
					return;
				}
				if (!this.title) {
					uni.showToast({
						title: '请填写议题标题',
						icon: 'none',
						duration: 1000
					});
					return;
				}
        console.log("=========isSync",this.isSync)
				let params = {
					meet_id: this.id,
					content: this.content,
          title:this.title,
          is_sys:this.isSync.length?this.isSync[0]:'0',
					images: JSON.stringify(this.images || [])
				};
        uploadRecord(params).then(res => {
					this.isLoading = false;
					console.log(res)
					if (res.code == 1) {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							});
						}, 2000)
					}
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					});
				})
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
				this.images.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log("result：", result)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					this.images.push(result)
					console.log("images:", this.images)
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: HTTP_REQUEST_URL + '/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log("res:", res)
							if (JSON.parse(res.data).code == 1) {
								setTimeout(() => {
									resolve(JSON.parse(res.data).data.files)
								}, 500)
							} else {
								uni.showToast({
									title: JSON.parse(res.data).msg,
									icon: 'none',
									duration: 1000
								});
							}

						}
					});
				})
			},
			// 新增文件
			async afterRead1(event) {
				let _this = this;
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise1(lists[i].url);
					console.log("result===>", result)
					var data = JSON.parse(result)

					if (data.code == 0) {
						_this.$toast(data.msg);
						this[`fileList${event.name}`].splice(event.index, 1)
						return false;
					}
          if(data.data){
            this.form.files = data.data.files;
            this.show_files = true;
            let item = this[`fileList${event.name}`][fileListLen]
            this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
              status: 'success',
              message: '',
              url: result
            }))
            this[`fileList${event.name}`].splice(fileListLen, 1)
            fileListLen++
          }
				}
			},
			uploadFilePromise1(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$httpApi+'/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'hy'
						},
						success: (res) => {



							console.log("uploadFile==>", res)
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},
			deletePic1() {
				this.fileList1.splice(event.index, 1)
				this.files = null;
				this.fileName = null;
			}

		}
	}
</script>

<style>
.tishititle{
  font-size: 20rpx;
  color: #ccc;
  padding-top: 20rpx;
}
	.content {
		background-color: #F8F8F8;
		min-height: 100vh;
		padding: 30rpx;
	}

	.img-box {
		margin: 30rpx 0;
	}

	/deep/.u-upload__button {
		background-color: #fff;
	}

	.file-box {
		width: 100%;
	}

	.file-tip {
		background-color: #fff;
		padding: 30rpx;
		width: 620rpx;
		font-size: 28rpx;
		font-weight: 500;
		color: #999999;
	}

	.button {
		background: #4871C0;
		border-radius: 8rpx;
		opacity: 1;
		font-size: 28rpx;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
		padding: 30rpx 0;
		position: fixed;
		width: calc(100% - 60rpx);
		bottom: 10rpx;
		left: 30rpx;
	}

	/deep/.file-box .u-upload__wrap__preview {
		width: 670rpx !important;
		/* height: 88rpx!important; */
	}

	/deep/.file-box .u-upload__button .u-icon {
		display: none !important;
	}

	/deep/.file-box .u-upload__wrap__preview .u-icon {
		display: none !important;
	}

	.file-name {
		padding: 10px;
		background: #F8F9FC;
		border-radius: 8px 8px 8px 8px;
		position: relative;
	}

	.new-tip {
		position: absolute;
		right: 0px;
		top: 0;
		width: 30px;
		text-align: center;
		height: 16px;
		background: #FF7A00;
		border-radius: 0px 8px 0px 4px;
		font-size: 8px;
		color: #ffffff;
	}

	.pic_user {
		width: 38px;
		height: 38px;
		margin-bottom: 20px;
		background: #F0F0F0;
		border-radius: 4px 4px 4px 4px;
		background: #F0F0F0 url('@/static/assets/images/pick_user.png') no-repeat center center;
		background-size: 40% 40%;
	}


	.file-name {
		padding: 10px;
		background: #fff;
		border-radius: 8px 8px 8px 8px;
		position: relative;
	}

	.u-flex {
		display: flex;
		align-items: center;
	}

	.file-name {
		padding: 10px;
		background: #fff;
		border-radius: 8px 8px 8px 8px;
		display: flex;
		position: relative;
		width: 86vw;
		font-size: 30rpx;
		color: #4871C0;
		/* margin-left: 3.5vw; */

	}

	.file-txt {
		width: 80%;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.content-box {
		width: calc(100vw - 60rpx);
		min-height: 92rpx;
		background-color: #fff;
	}

	.file-tip {
		background-color: #fff;
		padding: 30rpx;
		width: calc(100vw - 48pt);
		font-size: 28rpx;
		font-weight: 500;
		color: #999999;
	}
</style>
